﻿<?php
header("content-type:text/html;charset=utf-8"); 
$pdo= new PDO('mysql:host=localhost;dbname=1606phpe;','root','root');
$sql="select id,name,price,starttime,endtime from admin";
$data=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$starttime = $data[0]['starttime'];
$starttime = strtotime($starttime);
$now = strtotime(date("Y-m-d H:i:s"));
$time = $starttime-$now;
?>
<html>
<head>
	<title></title>
</head>
<body>
<?php foreach($data as $value){?>
<div style='border:1px solid red;float:left;margin:10px;padding:20px;'>
	<div>倒计时：<span id="hh"></span>小时 <span id="mm"></span>分 <span id="ss"></span>秒</div>
	<div><img src=""></div>
	<div><?=$value['name']?></div>
	<div><?=$value['price']?></div>
	<div><input type='button' value='立即抢购' disabled='true' id='btn' class='btn'></div>
</div>
<?php } ?>
</body>
</html>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
countDown(<?php echo $time?>);
function countDown(times,id){
	var timer=null;		// 计时器
	timer=setInterval(
		function(){
			var day=0,hour=0,minute=0,second=0;	//时间默认值
			
			if(times<=0){
				clearInterval(timer);		// 清除计时器
				// 让按钮生效，可以点按钮
				$('.btn').attr('disabled',false);
			}

			if(times > 0){
				//$('.s-buy').on('click',function(){return false;});
				day = Math.floor(times / (60 * 60 * 24));	// 获取天
				hour = Math.floor(times / (60 * 60)) - (day * 24);	// 获取小时
				minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);		// 分钟
				second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);	// 秒
			}

			if (day <= 9) day = '0' + day;
			if (hour <= 9) hour = '0' + hour;
			if (minute <= 9) minute = '0' + minute;
			if (second <= 9) second = '0' + second;
			//

			$('#hh').text(hour);
			$('#mm').text(minute);
			$('#ss').text(second);
			times--;
		}
		,1000);
}
</script>
